package com.yujian.functions;
import java.io.IOException;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.select.Elements;
/*
 * 目标html代码
 * 
<li class="sky skyid lv2 on"> sky skyid lv3 on
<h1>28日（今天）</h1>
<big class="png40 d01"></big>
<big class="png40 n07"></big>
<p title="多云转小雨" class="wea">多云转小雨</p>
<p class="tem">
<span>24</span>/<i>13℃</i>
</p>
<p class="win">
<em>
<span title="无持续风向" class=""></span>
<span title="无持续风向" class=""></span>
</em>
<i>微风</i>
</p>
<div class="slid"></div>
</li>
 */
public class Weather {
	protected static final String LINE_SEPARATOR = System.getProperty("line.separator");
	String todayWeather = null;
	public Document getDocument(String url) {
		try {
			return Jsoup.connect(url).get();
		} catch (IOException e) {
			e.printStackTrace();
		}
		return null;
	}
	public String getTodayWeather(){
		//取得今天的天气（广州城区）
		Weather t = new Weather();
		Document doc = t.getDocument("http://www.weather.com.cn/html/weather/101280101.shtml");
		// 获取目标HTML代码
		Elements elements1 = doc.select("li");
		// 今天
		Elements elements2 = elements1.select("h1");
		String today = elements2.get(0).text();
		System.out.println(today);
		// 是否有雨
		Elements elements3 = elements1.select("[class=wea]");
		String rain = elements3.get(0).text();
		System.out.println(rain);
		// 高的温度
		Elements elements4 = elements1.select("span");
		String highTemperature = elements4.get(1).text() + "/";
		// 低的温度
		Elements elements5 = elements1.select("i");
		String lowTemperature = elements5.get(0).text();
		System.out.println(highTemperature+lowTemperature);
		// 风力
		Elements elements6 = elements1.select("i");
		String wind = elements6.get(1).text();
		System.out.println(wind);
		todayWeather = "广州"+LINE_SEPARATOR+today+LINE_SEPARATOR+rain+LINE_SEPARATOR+highTemperature+lowTemperature+LINE_SEPARATOR+wind;
		return todayWeather;
	}
	public static void main(String[] args) {
		Weather we = new Weather();
		we.getTodayWeather();
	}
}
